//CSE 430 - Operating Systems
//Partha Dasgupta
//Spring '12
//Project 2

//Alan Tu
//Kelly Jesionowski

#include <stdio.h>
#include "threads.h"

int globVal = 0;

void f1(){
	int locVal;
	printf("** Starting function 1 **\n");
	sleep(1);
	yield();
    while(1){
        printf("Function 1 running...\n");
        sleep(1);
        printf("\nFunction 1 incrementing values by 1...\n");
        locVal = locVal+1;
        globVal = globVal+1;
        printf("\tLocal: %d\tGlobal: %d\n", locVal, globVal);
        sleep(1);
        printf("Yielding function 1...\n\n");
        sleep(1);
        yield();
    }
}

void f2(){
	int locVal;
	printf("** Starting function 2 **\n");
	sleep(1);
	yield();
    while(1){
        printf("Function 2 running...\n");
        sleep(1);
        printf("\nFunction 2 incrementing values by 2...\n");
        locVal = locVal+2;
        globVal = globVal+2;
        printf("\tLocal: %d\tGlobal: %d\n", locVal, globVal);
        sleep(1);
        printf("Yielding function 2...\n\n");
        sleep(1);
        yield();
    }
}

void f3(){
	int locVal;
	printf("** Starting function 3 **\n");
	sleep(1);
	yield();
    while(1){
        printf("Function 3 running...\n");
        sleep(1);
        printf("\nFunction 3 incrementing values by 3...\n");
        locVal = locVal+3;
        globVal = globVal+3;
        printf("\tLocal: %d\tGlobal: %d\n", locVal, globVal);
        sleep(1);
        printf("Yielding function 3...\n\n");
        sleep(1);
        yield();
    }
}

void f4(){
	int locVal;
	printf("** Starting function 4 **\n");
	sleep(1);
	yield();
    while(1){
        printf("Function 4 running...\n");
        sleep(1);
        printf("\nFunction 4 incrementing values by 4...\n");
        locVal = locVal+4;
        globVal = globVal+4;
        printf("\tLocal: %d\tGlobal: %d\n", locVal, globVal);
        sleep(1);
        printf("Yielding function 4...\n\n");
        sleep(1);
        yield();
    }
}

void main(){
    InitQueue(&readyQ);
    start_thread(f1);
    start_thread(f2);
    start_thread(f3);
    start_thread(f4);
    run();

    //Write a few functions with infinite loops (put a yield in each loop). Note: try to write thread functions that are meaningful, use global and local variables
    
    //In main, initialize ReadyQ, start threads using the functions you defined using start_thread.
    
    //Call run() and watch...
}
